package com.hh.controller;

import com.hh.dto.ResultDTO;
import com.hh.service.NewsService;
import com.hh.until.Tool;
import com.hh.vo.NewsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/back/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @GetMapping("/findAll")
    public ResultDTO findAll(NewsVO newsVO) {
        newsVO.setStart(Tool.nullToDateTime(newsVO.getStart()));
        newsVO.setEnd(Tool.nullToDateTime(newsVO.getEnd()));
        newsVO.setTitle(Tool.nullToStr(newsVO.getTitle()));
        newsVO.setTypeId(Tool.nullToInt(newsVO.getTypeId()));
        newsVO.setEnable(Tool.nullToInt(newsVO.getEnable()));
        newsVO.setPage(Tool.strToPage(newsVO.getPage().toString()));
        newsVO.setLimit(Tool.strToLimit(newsVO.getLimit().toString()));

        ResultDTO resultDTO = newsService.findAll(newsVO);
        return resultDTO;
    }

    @GetMapping("/findById")
    public ResultDTO findById(NewsVO newsVO) {
        newsVO.setId(Tool.strToInt(newsVO.getId().toString()));

        ResultDTO resultDTO = newsService.findById(newsVO);
        return resultDTO;
    }

    @PostMapping("/add")
    public ResultDTO add(NewsVO newsVO) {
        newsVO.setTime(Tool.nullToDate(newsVO.getTime()));
        newsVO.setTitle(Tool.nullToStr(newsVO.getTitle()));
        newsVO.setContent(Tool.nullToStr(newsVO.getContent()));
        newsVO.setTypeId(Tool.strToInt(newsVO.getTypeId().toString()));
        newsVO.setEnable(Tool.nullToInt(newsVO.getEnable()));
        newsVO.setImg(Tool.nullToStr(newsVO.getImg()));
        newsVO.setRemark(Tool.nullToStr(newsVO.getRemark()));

        ResultDTO resultDTO = newsService.add(newsVO);
        return resultDTO;
    }

    @PostMapping("/delete")
    public ResultDTO delete(NewsVO newsVO) {
        newsVO.setId(Tool.strToInt(newsVO.getId().toString()));

        ResultDTO resultDTO = newsService.delete(newsVO);
        return resultDTO;
    }

    @PostMapping("/update")
    public ResultDTO update(NewsVO newsVO) {
        newsVO.setId(Tool.strToInt(newsVO.getId().toString()));
        newsVO.setTime(Tool.nullToDate(newsVO.getTime()));
        newsVO.setTitle(Tool.nullToStr(newsVO.getTitle()));
        newsVO.setContent(Tool.nullToStr(newsVO.getContent()));
        newsVO.setTypeId(Tool.strToInt(newsVO.getTypeId().toString()));
        newsVO.setEnable(Tool.nullToInt(newsVO.getEnable()));
        newsVO.setImg(Tool.nullToStr(newsVO.getImg()));
        newsVO.setRemark(Tool.nullToStr(newsVO.getRemark()));

        ResultDTO resultDTO = newsService.update(newsVO);
        return resultDTO;
    }

    @GetMapping("/count")
    public ResultDTO count(NewsVO newsVO) {
        newsVO.setTitle(Tool.nullToStr(newsVO.getTitle()));
        newsVO.setEnable(Tool.nullToInt(newsVO.getEnable()));

        ResultDTO resultDTO = newsService.count(newsVO);
        return resultDTO;
    }
}
